package com.arif.app.sulamunnayirain;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

	private Button buttonAbout;
	private Button buttonHitung;
	private Spinner spinnerTahun;
	private Spinner spinnerBulan;
	private Spinner spinnerKota;
	private TextView textCopy;
	private int indexBulan;
	private String namaBulan;
	private int indexKota;
	private String namaKota;
	private int tahun;
	private HashMap<String, Integer> mapKota;
	private final String[] months = {"Muharram", "Safar", "RabiulAwwal",  
            "Rabi'ul Akhir", "Jumadal Ula", "Jumadal Akhira", "Rajab",  
            "Sha'ban", "Ramadan", "Shawwal", "Dhul Qa'ada", "Dhul Hijja"};
	private final String[] kota={
					"Anyar", 
					"Sigun",
					"Minas",
					"Rangkas", 
					"Serang",
					"Pandeglang", 
					"Bogor",
					"Jakarta", 
					"Bekasi",
					"Karawang", 
					"Purwakarta", 
					"Bandung",
					"Cirebon",
					"Yogyakarta", 
					"Semarang",
					"Jepara",
					"Solo",
					"Kudus",
					"Rembang", 
					"Kediri",
					"Surabaya", 
					"Jombang",
					"Pasuruan",
					"Probolinggo", 
					"Banyuwangi",
					"Sumenep",
					"Malang"
	};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        buttonAbout=(Button)findViewById(R.id.buttonAbout);
        buttonAbout.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View arg0) {
				Intent i=new Intent(MainActivity.this, AboutActivity.class);
				startActivity(i);
			}
		});
        buttonHitung=(Button)findViewById(R.id.buttonHitung);
        buttonHitung.setOnClickListener(new View.OnClickListener() {			
			@Override
			public void onClick(View arg0) {
				Intent i=new Intent(MainActivity.this, OutputActivity.class);
				i.putExtra(ConstantValue.KEY_BULAN, indexBulan);
				i.putExtra(ConstantValue.KEY_KOTA, indexKota);
				i.putExtra(ConstantValue.KEY_TAHUN, tahun);
				i.putExtra(ConstantValue.KEY_BULAN_NAME, namaBulan);
				i.putExtra(ConstantValue.KEY_KOTA_NAME, namaKota);
				startActivity(i);
			}
		});
        textCopy=(TextView)findViewById(R.id.textCopy);
        textCopy.setText(Html.fromHtml("&copy;2014 Sullamun Nayyirain<br>v1.0"));
        textCopy.setGravity(Gravity.CENTER);
        buttonAbout.setVisibility(Button.INVISIBLE);
        indexBulan=0;
        indexKota=0;
        tahun=1400;
        this.initCombo();
    }
    private void initCombo(){
    	mapKota=new HashMap<String, Integer>();
    	spinnerTahun = (Spinner) findViewById(R.id.spinner1);
		List<Integer> list = new ArrayList<Integer>();
		for(int i=tahun;i<=tahun+100;i++){
			list.add(i);	
		}		
		ArrayAdapter<Integer> dataAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item,list);
		dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerTahun.setAdapter(dataAdapter);
		spinnerTahun.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

				tahun=(Integer)spinnerTahun.getSelectedItem() ;				
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		spinnerBulan=(Spinner)findViewById(R.id.spinnerBulan);
		List<String> listBulan=new ArrayList<String>();
		for (int i = 0; i < months.length; i++) {
			listBulan.add(months[i]);
		}
		ArrayAdapter<String > bulanAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,listBulan);
		bulanAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerBulan.setAdapter(bulanAdapter);
		
		spinnerBulan.setOnItemSelectedListener(new OnItemSelectedListener() {
			
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
//				
				indexBulan= spinnerBulan.getSelectedItemPosition();
				namaBulan=(String)spinnerBulan.getSelectedItem();
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		
		spinnerKota=(Spinner)findViewById(R.id.spinnerKota);
		List<String> listKota=new ArrayList<String>();
		for (int i = 0; i < kota.length; i++) {
			mapKota.put(kota[i], i);
			listKota.add(kota[i]);
		}
		Collections.sort(listKota);
		
		ArrayAdapter<String> kotaAdapter=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,listKota);
		kotaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinnerKota.setAdapter(kotaAdapter);		
		spinnerKota.setOnItemSelectedListener(new OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {

				namaKota=(String)spinnerKota.getSelectedItem();
				indexKota= mapKota.get(namaKota);
				
			}
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub
				
			}
		});
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
